package com.sitech.portal.server.dao.ibatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sitech.ibnms.core.BaseIbnmsDao;
import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.PageForm;
import com.sitech.ibnms.util.RandomGUID;
import com.sitech.portal.server.dao.IPortalDao;

public class PortalDaoImpl extends BaseIbnmsDao implements IPortalDao {

	public List findAllPorlets() {
        return this.query("findAllPorlets");
    }
	
	public List findUserPorlets(String login_id) {
        return this.query("findUserPorlets",login_id);
    }

    public Map findPorletById(String porlet_id) {
        return (Map) this.querySingle("findPorletById", porlet_id);
    }

    public void insertPORLET(Map porlet) {
        this.insert("insertPORLET", porlet);
    }

    public void updatePORLET(Map porlet) {
        this.update("updatePORLET", porlet);
    }

    public void deletePORLETById(String porlet_id) {
        this.delete("deletePORLETById", porlet_id);
        deleteRoleMapOfPorlet(porlet_id);
    }

    public List findAllLayouts() {
        return this.query("findAllLayouts");
    }

    public Map findLayoutById(String layout_id) {
        return (Map) this.query("findLayoutById", layout_id).get(0);
    }

    public List findPorletsByROLE_ID(List<Map> ROLE_IDs) {
        String param = "'defalut'";
        if (ROLE_IDs != null && !ROLE_IDs.isEmpty()) {
            for (int i = 0; i < ROLE_IDs.size(); i++) {
                param += ",'" + ROLE_IDs.get(i).get("ROLE_ID") + "'";
            }
        }
        return this.query("findPorletsByROLE_ID", param);
    }
    
    public void insertUserSetting(HashMap map){
    	this.insert("insertUserSetting", map);
    }

    public Map findUserSetting(String LOGIN_ID) {
        List list = this.query("findUserSetting", LOGIN_ID);
//        if (list.isEmpty()) {
//            list = this.query("findUserSetting",
//                    "default");
//        }
//        Map map = (Map) list.get(0);
//        if (!LOGIN_ID.equals(map.get("LOGIN_ID"))) {
//            map.put("LOGIN_ID", LOGIN_ID);
//            this.insert("insertUserSetting", list.get(0));
//        }
        if(list!=null && list.size()>0)
        	return (Map) list.get(0);
        return null;
    }

    public int updateUserSetting_PORLETS(String LOGIN_ID, String PORLETS) {
        HashMap param = new HashMap();
        param.put("LOGIN_ID", LOGIN_ID);
        param.put("PORLETS", PORLETS);
        return this.update("updateUserSetting_PORLETS", param);
    }

    public int updateUserSetting_LAYOUT(String LOGIN_ID, String LAYOUT_ID) {
        HashMap param = new HashMap();
        param.put("LOGIN_ID", LOGIN_ID);
        param.put("LAYOUT_ID", LAYOUT_ID);
        return this.update("updateUserSetting_LAYOUT", param);
    }

    public void updateRoleMapOfPorlet(String PORLET_ID, String[] ROLE_ID) {
        deleteRoleMapOfPorlet(PORLET_ID);
        for (int i = 0; i < ROLE_ID.length; i++) {
            insertRoleMapOfPorlet(PORLET_ID, ROLE_ID[i].trim());
        }
    }

    public void insertRoleMapOfPorlet(String PORLET_ID, String ROLE_ID) {
        HashMap param = new HashMap();
        param.put("PORLET_ID", PORLET_ID);
        param.put("ROLE_ID", ROLE_ID);
        this.insert("insertRoleMapOfPorlet", param);
    }

    public void deleteRoleMapOfPorlet(String PORLET_ID) {
        this.insert("deleteRoleMapOfPorlet", PORLET_ID);
    }

	@Override
	public String findPortalHelper(String helpId) {
		HashMap<String,String> param = new HashMap<String,String>();
		param.put("LOGIN_ID", "");
		param.put("HELP_ID", helpId);
		
		return (String)this.querySingle("findPortalHelper", param);
	}

	@Override
	public void savePortalHelper(String helpId,
			String helpMsg) {
		HashMap<String,String> param = new HashMap<String,String>();
		param.put("GUID", RandomGUID.getRandomGUID());
		param.put("LOGIN_ID", "");
		param.put("HELP_ID", helpId);
		param.put("HELP_MSG", helpMsg);
		insert("savePortalHelper", param);
	}

	@Override
	public boolean updatePortalHelper(String helpId,
			String helpMsg) {
		HashMap<String,String> param = new HashMap<String,String>();
		param.put("LOGIN_ID", "");
		param.put("HELP_ID", helpId);
		param.put("HELP_MSG", helpMsg);
		update("updatePortalHelper", param);
		return false;
	}

	@Override
	public IPage findAllPorletsList(PageForm pageForm) {
		// TODO Auto-generated method stub
		return this.findPage("findAllPorlets", pageForm);
	}

	@Override
	public List findUserSettingPorlets(String login_id) {
		// TODO Auto-generated method stub
		return this.query("findUserSettingPorlets", login_id);
	}
	
}
